package com.huizhonglm.diap.ui.user;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.huizhonglm.diap.core.utils.LogUtil;
import com.huizhonglm.diap.ui.BaseActivity;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.widget.LoadView;


public class PrivacyRulesActivity extends BaseActivity {

    private WebView mWebView;
    private LoadView loadView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy_rules);
        setBackEvent(this, null);
        assignViews();
    }

    @Override
    protected void assignViews() {
        mWebView = (WebView) findViewById(R.id.privacy_rules_web);
        loadView = (LoadView) findViewById(R.id.load_view);
        mWebView.setBackgroundColor(0);
        Drawable bg = mWebView.getBackground();
        if (bg != null) {
            bg.setAlpha(0);
        }
        mWebView.setWebViewClient(mWebViewClient);
        mWebView.setWebChromeClient(mWebChromeClient);
        loadView.setDataView(mWebView, mReLoadDataListener);
        loadHtmlData();
    }

    private void loadHtmlData() {
        loadView.showProgress();
        String url = "file:///android_asset/privacy_rules.html";
        mWebView.clearCache(true);
        mWebView.clearHistory();
        mWebView.clearFormData();
        mWebView.loadUrl(url);
    }


    private final LoadView.OnLoadAgainListener mReLoadDataListener = new LoadView.OnLoadAgainListener() {
        @Override
        public void onLoadAgain() {
            loadHtmlData();
        }
    };

    public final WebChromeClient mWebChromeClient = new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            LogUtil.print("onProgressChanged----------loadProgress=" + newProgress + "%");
        }
    };

    public WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            loadView.showErrorView();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            loadView.showDataView();
        }
    };

    @Override
    public void finish() {
        if (mWebView != null) {
            mWebView.clearHistory();
            mWebView.clearAnimation();
            mWebView.clearCache(true);
            mWebView.clearDisappearingChildren();
            mWebView.clearFormData();
            mWebView.clearMatches();
            mWebView.clearFocus();
            mWebView.clearSslPreferences();
            mWebView.clearView();
        }
        super.finish();
    }
}
